<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="物料料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielCode', validatorRules.materielCode]" placeholder="请输入物料料号"></a-input>
        </a-form-item>
        <a-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName']" placeholder="请输入物料名称"></a-input>
        </a-form-item>
        <a-form-item label="物料规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielGague']" placeholder="请输入物料规格"></a-input>
        </a-form-item>
        <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['supplier']" placeholder="请输入供应商"></a-input>
        </a-form-item>
        <a-form-item label="有效起始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择有效起始时间" v-decorator="['validStartime', validatorRules.validStartime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="有效结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择有效结束时间" v-decorator="['validEndtime', validatorRules.validEndtime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['appendix']" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="申请原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['applyReason']" rows="4" placeholder="请输入申请原因"/>
        </a-form-item>
        <a-form-item label="申请依据" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['applyBasis']" rows="4" placeholder="请输入申请依据"/>
        </a-form-item>
        <a-form-item label="评审意见" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['reviewComment']" rows="4" placeholder="请输入评审意见"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JUpload from '@/components/jeecg/JUpload'


  export default {
    name: "MesChiefdataDispensecheckModal",
    components: { 
      JDate,
      JUpload,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          materielCode: {
            rules: [
              { required: true, message: '请输入物料料号!'},
            ]
          },
          validStartime: {
            rules: [
              { required: true, message: '请输入有效起始时间!'},
            ]
          },
          validEndtime: {
            rules: [
              { required: true, message: '请输入有效结束时间!'},
            ]
          },
        },
        url: {
          add: "/chiefdata/mesChiefdataDispensecheck/add",
          edit: "/chiefdata/mesChiefdataDispensecheck/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'materielCode','materielName','materielGague','supplier','validStartime','validEndtime','appendix','applyReason','applyBasis','reviewComment'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'materielCode','materielName','materielGague','supplier','validStartime','validEndtime','appendix','applyReason','applyBasis','reviewComment'))
      },

      
    }
  }
</script>